import axios from 'axios'
import router from '@/router'
import { Dialog } from 'vant'
const requests = axios.create({
  baseURL: '/api',
  timeout: 10000
})
//请求拦截器
requests.interceptors.request.use(
  (res) => {
    if (localStorage.token) {
      res.headers.token_ = localStorage.token
    }
    return res
  },
  (error) => {
    return Promise.reject(error)
  }
)
/* 错误类型映射 */
// const msgErr = {
//   500: '网络错误，请稍后重试！',
//   502: '系统重启中···!',
//   404: '访问地址不存在，或待更新！',
//   400: '客户端错误！'
// }
//响应拦截器
requests.interceptors.response.use(
  (res) => {
    // if (data.data.statusCode === 0 && !localStorage.token) {
    //   Dialog.confirm({
    //     message: '没登录，登录吗'
    //   })
    //     .then(() => {
    //       router.push('/login')
    //     })
    //     .catch(() => {
    //       console.log(1)
    //     })
    // }
    if(res.data.status!==20001 && localStorage.status!=20001){
      Dialog.confirm({
            message: '没登录，登录吗',
          })
            .then(() => {
              localStorage.clear()
              router.push('/login')
            })
            .catch(() => {
              console.log(1)
            })
    }
    return res
  },
  (error) => {
    return Promise.reject(error)
  }
)
export default requests
